package com.qf.web.controller;

import com.alibaba.druid.util.StringUtils;
import com.qf.dao.StuDao;
import com.qf.dao.impl.StuDaoImpl;
import com.qf.service.StuService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * projectName:lichengyu
 * author:lcy
 * time:2024/4/30 11:03
 * description:
 */
@WebServlet("/deleteStu")
public class DeleteStuServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        if(StringUtils.isEmpty(id)){
            request.setAttribute("msg", "错误");
            request.getRequestDispatcher("message.jsp").forward(request,response);
            return;
        }
        StuDao stuDao=new StuDaoImpl();
        try {
            int bid=Integer.parseInt(id);
            stuDao.remove(bid);
            response.sendRedirect("selectstuservlet");
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            request.setAttribute("msg", "删除失败"+e.getMessage());
            request.getRequestDispatcher("message.jsp").forward(request,response);

        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}


